var $table = $('#table');
var URI_LIST = "/sec/attachment/list";
var URI_DEL = "/sec/attachment/delete/";
var URI_DETAIL = "/sec/attachment/detail/";
var URL_NAME_CHECK = "/sec/attachment/check";
var fieldCloumns = [
    {field: 'ck', checkbox: true},
    {field: 'objectName', title: '对象名'},
    {field: 'objectKey', title: '对象key'},
    {field: 'name', title: '附件名称'},
    {field: 'mime', title: '附件MIME'},
    {field: 'fileDesc', title: '说明信息'},
    {field: 'uri', title: '附件URI'},
    {field: 'code', title: '验证值'},
    {field: 'delFlag', title: '删除标志'},
    {field: 'aid', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
]

function initValidate(formId) {
    var validator = $(formId).validate({
        rules: {
            objectName: {required: true, maxlength: 100},
            objectKey: {required: true, maxlength: 100},
            name: {required: true, maxlength: 100},
            mime: {required: true, maxlength: 100},
            fileDesc: {required: true, maxlength: 500},
            uri: {required: true, maxlength: 200},
            code: {required: true, maxlength: 100},
            delFlag: {required: true, maxlength: 2}
        }
    });
    return validator;
}

var idField = "aid";
$(function () {
    $table.bootstrapTable({
        url: URI_LIST,
        detailFormatter: 'detailFormatter',
        idField: idField,
        toolbar: '#toolbar',
        columns: fieldCloumns
    });
});

function detailFormatter(index, row) {
    $.get(URI_DETAIL + row.aid + ".html", function (resp) {
        $("tr[data-index=" + index + "]").next(".detail-view").children("td").html(resp);
    });
}

// 格式化操作按钮
function actionFormatter(value, row, index) {
    return [
        '<a class="delete" href="javascript:;" onclick="deleteAction()" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>'
    ].join('');
}

// 删除
var deleteDialog;

function deleteAction(value) {
    var rows = getRows(value);
    if (rows.length == 0) {
        showMessage("请选择一条记录！", "error");
    } else {
        deleteDialog = $.confirm({
            type: 'red',
            animationSpeed: 300,
            title: false,
            content: '确认删除该记录吗？',
            buttons: {
                confirm: {
                    text: '确认',
                    btnClass: 'waves-effect waves-button',
                    action: function () {
                        var ids = new Array();
                        for (var i in rows) {
                            ids.push(rows[i].gid);
                        }
                        $.ajax({
                            type: 'post',
                            url: URI_DEL + ids.join("-"),
                            success: function (result) {
                                if (result.code != "0000") {
                                    showMessage(result.code || result.desc, "error");
                                } else {
                                    deleteDialog.close();
                                    showMessage("删除成功");
                                    $table.bootstrapTable('refresh');
                                }
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
    }
}

function userAddAction(value) {
    var rows = getRows(value);
    if (rows.length == 0) {
        showMessage("请选择一条记录！", "error");
    } else {
        loadDialog("#rolesModal", "增加用户授权", "/sec/attachment/users.html?fid="+rows[0].aid, function () {
            var attUserValdator = $("#attachmentForm").validate({
                rules: {
                    uid: {required: true, remote: URL_NAME_CHECK}
                }
            });
            $("#attachmentForm").ajaxForm({
                target: '#btn-SecAtt-user',
                dataType: 'json',
                beforeSubmit: function (formData, jqForm, options) {
                    return attUserValdator.valid();
                },
                url: "/sec/attachment/users",
                success: function (result, statusText, xhr, $form) {
                    if (result.code != "0000") {
                        showMessage(result.code || result.desc, "error");
                    } else {
                        $("#rolesModal").modal("hide");
                        showMessage("设置访问用户成功!")
                    }
                }
            });
        });
    }
}

function loadDialog(selectEL, title, loadURI, success) {
    $(selectEL).find(".modal-title").html(title);
    $(selectEL).find('.modal-body').load(loadURI, function (response, status, xhr) {
        $('.selectpicker').selectpicker({
            noneSelectedText: "--请选择--",
            selectOnTab: true,
            liveSearch: true
        });
        $(selectEL).modal("show");
        if (success) {
            success();
        }
    });
}

function getRows(value) {
    var rows = []
    if (value) {
        rows.push($table.bootstrapTable('getRowByUniqueId', value));
    } else {
        rows = $table.bootstrapTable('getSelections');
    }
    return rows;
}